package com.netty.demo.server.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * properties文件工具类
 *
 * @author Administrator 余秀良
 * @version 1.0
 * <p>date: 2019年09月19日  18:43 </p>
 * <p>address: 四川成都 </p>
 */
@Slf4j
public final class PropertiesUtil {
    private static final String SUFFIX = ".properties";

    /**
     * 初始化 Properties
     *
     * @param fileName .properties 文件名称
     * @return {@link Properties}
     * @author 余秀良
     * @version 1.0
     * <p>  date: 2019 - 09 - 27  15:28    </p>
     * <p>  address: 四川成都  </p>
     */
    private static Properties init(String fileName) {
        FileInputStream fileInputStream = null;
        try {
            if (StringUtil.isNotEmpty(fileName)) {
                if (!fileName.endsWith(SUFFIX)) {
                    fileName = fileName + SUFFIX;
                }
                //请求头
                File file = ResourceUtils.getFile("classpath:" + fileName);
                fileInputStream = new FileInputStream(file);
                Properties properties = new Properties();
                properties.load(fileInputStream);
                return properties;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * @param fileName 文件名
     * @param key      需要查询的key
     * @return 返回查询的对象
     */
    public static Object get(String fileName, String key) {
        Properties properties = init(fileName);
        assert properties != null;
        return properties.get(key);
    }

    /**
     * @param fileName 文件名
     * @param key      需要查询的key
     * @return 返回查询的对象
     */
    public static String getProperty(String fileName, String key) {
        Properties properties = init(fileName);
        assert properties != null;
        return properties.getProperty(key);
    }

    /**
     * @param fileName 文件名
     * @param key      键
     * @param value    值
     */
    public static void set(String fileName, String key, String value) {
        try {
            if (StringUtil.isNotEmpty(fileName)) {
                if (!fileName.endsWith(SUFFIX)) {
                    fileName = fileName + SUFFIX;
                }
                Properties properties = init(fileName);
                //请求头
                File file = ResourceUtils.getFile("classpath:" + fileName);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                assert properties != null;
                properties.put(key, value);
                properties.store(fileOutputStream, "insert or update");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
